<?php
/**
 * 创建数据trait
 * @author yupoxiong<i@yupoxiong.com>
 */

declare (strict_types=1);

namespace app\admin\traits;

use support\Response;

trait AdminCreateDataTrait
{

    public function createData($model, $validate): Response
    {
        $param = (request())->all();

        $check = $validate->scene('admin_add')->check($param);
        if (!$check) {
            return admin_error($validate->getError());
        }

        $result   = $model::create($param);
        $redirect = isset($param['_create']) && (int)$param['_create'] === 1 ? URL_RELOAD : URL_BACK;

        return $result ? admin_success('添加成功', [], $redirect) : admin_error('添加失败');

    }
}
